<!--
 * @Author: 郭招娣 10615594+guo-zhaodi@user.noreply.gitee.com
 * @Date: 2022-08-18 14:17:45
 * @LastEditors: 郭招娣 10615594+guo-zhaodi@user.noreply.gitee.com
 * @LastEditTime: 2022-08-18 21:21:06
 * @FilePath: \后端管理系统\app\src\views\Users\component\UserDialog.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <el-dialog  :before-close="closeDialog" :title="type?'编辑用户':'添加用户'" :visible.sync="dialogFormVisible" width="30%">
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      class="demo-ruleForm"
    >
      <el-form-item label="用户名" prop="username" label-width="80px">
        <el-input v-model="ruleForm.username" ></el-input>
      </el-form-item>
      <el-form-item  v-if="!type" label="用户密码" prop="password" label-width="80px">
        <el-input v-model="ruleForm.password"></el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="email" label-width="80px">
        <el-input v-model="ruleForm.email"></el-input>
      </el-form-item>
         <el-form-item label="手机号" prop="mobile" label-width="80px">
        <el-input v-model="ruleForm.mobile"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="resect(ruleForm)">取 消</el-button>
      <el-button type="primary"  @click="submitForm('ruleForm')">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  props: {
     //弹框开关
    dialogFormVisible: {
      type: Boolean,
      default: false,
    },
    type:{
      type:Boolean,
      default:false
    },
    // 信息
    ruleForm:{
      type:Object,
      default:()=>{
        return {
          username:'',
          password:'',
          email:'',
          mobile:''
        }
      }
    },

    
  },

  data() {
    return {
      rules:{
        username:[
             { required: true, message: '请填写用户名', trigger: 'blur' }
        ],
         password:[
             { required: true, message: '请填写用户密码', trigger: 'blur' }
        ],
         email:[
             { required: true, message: '请填写邮箱', trigger: 'blur' }
        ],
         mobile:[
             { required: true, message: '请填写手机号', trigger: 'blur' }
        ],
      }
    };
  },

  methods: {
     submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.$emit('save-form')
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
        resetForm(formName) {
        this.$refs[formName].resetFields();
      },
      // 关闭x弹框
      closeDialog(){
      this.$emit('close-dialog')
    },
      // 关闭取消弹框
    resect(){
      this.$emit('close-dialog')
    }
  },
};
</script>